﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Deepseek_Robot.Modules.Utils
{
    class VideoRecorder
    {
        public static void RecordVideo(string outputPath, int durationInSeconds)
        {
            try
            {
                // FFmpeg 可执行文件的路径
                string ffmpegPath = @"C:\ffmpeg\bin\ffmpeg.exe";
                // 录制屏幕的命令
                string command = $"-f gdigrab -i desktop -t {durationInSeconds} {outputPath}";

                // 创建进程启动信息
                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = ffmpegPath,
                    Arguments = command,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                // 创建并启动进程
                using (Process process = new Process { StartInfo = startInfo })
                {
                    process.Start();
                    process.WaitForExit();

                    // 检查是否有错误输出
                    string errorOutput = process.StandardError.ReadToEnd();
                    if (!string.IsNullOrEmpty(errorOutput))
                    {
                        Console.WriteLine($"FFmpeg 错误: {errorOutput}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"录制视频时发生错误: {ex.Message}");
            }
        }

        // 导入 Windows API 函数
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        // 常量定义
        private const uint BM_CLICK = 0x00F5;

      

        public static void RecordSimulation()
        {
            try
            {
                // 查找 RobotStudio 主窗体
                IntPtr robotStudioHandle = FindWindow(null, "RSPACK_0602_6.08 - ABB RobotStudio 6.08");
                if (robotStudioHandle == IntPtr.Zero)
                {
                    Console.WriteLine("未找到 RobotStudio 窗体。");
                    return;
                }
        IntPtr toolBarHandle = (IntPtr)0x00151406;
             IntPtr recordButtonHandle = FindWindowEx(robotStudioHandle, toolBarHandle, null, "仿真");
                if (recordButtonHandle == IntPtr.Zero)
                {
                    Console.WriteLine("未找到“仿真录像”按钮。");
                    return;
                }

                // 模拟点击按钮
                SendMessage(recordButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
                Console.WriteLine("已点击“仿真录像”按钮。");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
        }
    }
}
